{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9b6162ff-28c5-4566-ae44-c8fdffd214dc",
   "metadata": {},
   "source": [
    "# 芯片检测练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cdf6664d-8193-4491-98a4-8a6de371fe0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 读取数据\n",
    "data = pd.read_csv('chip_test.csv')\n",
    "data.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1a98ba7-33fb-49f1-a284-6125111efd82",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可视化数据\n",
    "# 启用嵌入显示功能\n",
    "\n",
    "%matplotlib inline \n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "fig1 = plt.figure()\n",
    "e1 = data.loc[:,'test1']\n",
    "e2 = data.loc[:,'test2']\n",
    "\n",
    "plt.scatter(e1, e2, c = 'b')\n",
    "plt.title('test1 - test2')\n",
    "plt.xlabel('test1')\n",
    "plt.ylabel('test2')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce5d8a66-3f05-43fa-ab2b-7d969a23c725",
   "metadata": {},
   "outputs": [],
   "source": [
    "# add lable mask\n",
    "mask = data.loc[:,'Pass'] == 1\n",
    "print(mask)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d72f6205-12fb-4b32-88b5-44370d4c786f",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig2 = plt.figure(facecolor='w')\n",
    "\n",
    "plt.gca().set_facecolor('black') \n",
    "# 根据上面的标签mask来区分不同的点，如果不给出颜色，也是自动换成连个颜色的\n",
    "passed = plt.scatter(data.loc[:,'test1'][mask], data.loc[:,'test2'][mask], c = 'green')\n",
    "failed = plt.scatter(data.loc[:,'test1'][~mask], data.loc[:,'test2'][~mask], c = 'red')\n",
    "plt.legend((passed,failed),('passed','failed'))\n",
    "\n",
    "plt.title('test1(X-Label) - test2(Y-Label)')\n",
    "plt.xlabel('test1')\n",
    "plt.ylabel('test2')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01000053-f27c-4c08-9274-df1dc2fcc3e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义数据，用于训练\n",
    "X = data.drop(['Pass'],axis = 1) # 从data中剥离结果\n",
    "y = data.loc[:,'Pass']\n",
    "\n",
    "X1 = data.loc[:,'test1']\n",
    "X2 = data.loc[:,'test2']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb8c71c0-87f8-4824-b9ac-82f0fad304ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(X.shape, y.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91ffc24a-88a0-417f-8bff-cfdb30aa9a94",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 二级边界函数\n",
    "# 创建新的数据\n",
    "\n",
    "X1_2 = X1 * X1\n",
    "X2_2 = X2 * X2\n",
    "X1_X2 = X1 * X2\n",
    "X_new = {'X1':X1,'X2':X2,'X1_2':X1_2,'X2_2':X2_2,'X1_X2':X1_X2}\n",
    "X_new = pd.DataFrame(X_new)\n",
    "print(X_new)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e52ebc2-449e-4fe3-8c89-803dac19c649",
   "metadata": {},
   "outputs": [],
   "source": [
    "#创建新的模型\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "LR2 = LogisticRegression()\n",
    "LR2.fit(X_new, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f826b79-0058-4bf5-b611-928c921852ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "y2_predict = LR2.predict(X_new)\n",
    "print(y2_predict)\n",
    "\n",
    "from sklearn.metrics import accuracy_score\n",
    "accuracy2 = accuracy_score(y, y2_predict)\n",
    "print(accuracy2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f158ca12-6da3-4ffa-9084-e63b83ce4aa8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可视化\n",
    "LR2.coef_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f297f8c-b3ca-43bd-9bd9-b7650dccdadd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x1):\n",
    "    x1_sort = np.sort(x1)\n",
    "    a = theta4\n",
    "    b = theta5 * x1_sort + theta2\n",
    "    c = theta0 + theta1 * x1_sort + theta3 * x1_sort * x1_sort\n",
    "    X2_new_boundary1 = (-b+np.sqrt(b*b-4*a*c))/(2*a)\n",
    "    X2_new_boundary2 = (-b-np.sqrt(b*b-4*a*c))/(2*a)\n",
    "    return X2_new_boundary1,X2_new_boundary2\n",
    "\n",
    "\n",
    "def ff(x1):\n",
    "    # 确保输入是numpy数组并排序\n",
    "    x1 = np.array(x1)\n",
    "    x1_sort = np.sort(x1)\n",
    "    \n",
    "    # 计算二次方程系数\n",
    "    a = theta4\n",
    "    b = theta5 * x1_sort + theta2\n",
    "    c = theta0 + theta1 * x1_sort + theta3 * (x1_sort ** 2)\n",
    "    \n",
    "    # 计算判别式\n",
    "    discriminant = b**2 - 4*a*c\n",
    "    \n",
    "    # 创建结果数组，初始化为NaN\n",
    "    X2_new_boundary1 = np.full_like(x1_sort, np.nan)\n",
    "    X2_new_boundary2 = np.full_like(x1_sort, np.nan)\n",
    "    \n",
    "    # 只对判别式非负的位置计算平方根\n",
    "    valid_indices = discriminant >= 0\n",
    "    if np.any(valid_indices):\n",
    "        sqrt_discriminant = np.sqrt(discriminant[valid_indices])\n",
    "        X2_new_boundary1[valid_indices] = (-b[valid_indices] + sqrt_discriminant) / (2*a)\n",
    "        X2_new_boundary2[valid_indices] = (-b[valid_indices] - sqrt_discriminant) / (2*a)\n",
    "    \n",
    "    return X2_new_boundary1, X2_new_boundary2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d6a6ac7-d946-4ae3-b430-7348386d0bf5",
   "metadata": {},
   "outputs": [],
   "source": [
    "theta0 = LR2.intercept_\n",
    "theta1 = LR2.coef_[0][0]\n",
    "theta2 = LR2.coef_[0][1]\n",
    "theta3 = LR2.coef_[0][2]\n",
    "theta4 = LR2.coef_[0][3]\n",
    "theta5 = LR2.coef_[0][4]\n",
    "print(theta0,theta1,theta2,theta3,theta4,theta5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a85802da-8602-4827-9943-25aa8e879e40",
   "metadata": {},
   "outputs": [],
   "source": [
    "X1_sort = X1.sort_values()\n",
    "X2_new_boundary1,X2_new_boundary2 = ff(X1_sort)\n",
    "print(X1.head())\n",
    "fig7 = plt.figure()\n",
    "plt.plot(X1_sort,X2_new_boundary1)\n",
    "plt.plot(X1_sort,X2_new_boundary2)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bc23a85-89d2-4c93-8a2b-19f406de69db",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig8 = plt.figure()\n",
    "\n",
    "X2_new_boundary1,X2_new_boundary2 = f(X1)\n",
    "\n",
    "passed = plt.scatter(data.loc[:,'test1'][mask], data.loc[:,'test2'][mask],c='green')\n",
    "failed = plt.scatter(data.loc[:,'test1'][~mask], data.loc[:,'test2'][~mask],c='pink')\n",
    "plt.title('test1 - test2')\n",
    "plt.xlabel('test1')\n",
    "plt.ylabel('test2')\n",
    "plt.legend((passed,failed),('passed','failed'))\n",
    "plt.plot(X1_sort,X2_new_boundary1,c='r')\n",
    "plt.plot(X1_sort,X2_new_boundary2,c='r')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29ba9b9d-e86f-46ce-993d-a97052bb4284",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig9 = plt.figure()\n",
    "X1_range = [-0.7 + x / 10000 for x in range(0,20000)]\n",
    "X2_new_boundary1,X2_new_boundary2 = ff(X1_range)\n",
    "plt.plot(X1_range,X2_new_boundary1,c='r')\n",
    "plt.plot(X1_range,X2_new_boundary2,c='r')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45866ea6-0ec4-4430-a799-cd77ecff1f5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig9 = plt.figure()\n",
    "passed = plt.scatter(data.loc[:,'test1'][mask], data.loc[:,'test2'][mask],c='green')\n",
    "failed = plt.scatter(data.loc[:,'test1'][~mask], data.loc[:,'test2'][~mask],c='pink')\n",
    "plt.title('test1 - test2')\n",
    "plt.xlabel('test1')\n",
    "plt.ylabel('test2')\n",
    "plt.legend((passed,failed),('passed','failed'))\n",
    "\n",
    "X1_range = [-0.7 + x / 10000 for x in range(0,20000)]\n",
    "X2_new_boundary1,X2_new_boundary2 = ff(X1_range)\n",
    "plt.plot(X1_range,X2_new_boundary1,c='r')\n",
    "plt.plot(X1_range,X2_new_boundary2,c='r')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f55c2010-1cd6-4f1a-a320-7bb619ad84b9",
   "metadata": {},
   "source": [
    "* 边界函数\n",
    "$$\n",
    "w_1 x_1 + w_2 x_2 + b = 0\n",
    "$$\n",
    "\n",
    "* 二级边界函数\n",
    "$$\n",
    "\\theta_0 + \\theta_1 x_1 + \\theta_2 x_2 + \\theta_3 x_1^2 + \\theta_4 x_2^2 + \\theta_5 x_1 x_2 = 0\n",
    "$$\n",
    "\n",
    "* 二阶边界函数的标准形式\n",
    "$$\n",
    "\\theta_3 x_1^2 + (\\theta_1 + \\theta_5 x_2) x_1 + (\\theta_0 + \\theta_2 x_2 + \\theta_4 x_2^2) = 0\n",
    "$$\n",
    "\n",
    "* 求解x\n",
    "$$\n",
    "x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}\n",
    "$$\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ad4aa4c-5547-4dfb-bc93-43080d9a0ef0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1856a05a-442f-4731-a815-c0930f52b372",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42b9ceb3-f172-43df-9e86-8b7ec6c98394",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26a0c285-a5c4-4426-80b8-0ee3e5d9d391",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "e79c9a32-5034-4c48-af14-0ef5abb97cdf",
   "metadata": {},
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04f60036-2a6e-4be1-9172-d33293ae1d45",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18df5483-13b8-4d3a-8928-bfbdf7aa3b2a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "232054dd-820c-4381-b327-9aa77e693f78",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce6baf49-c4ab-4f1f-850e-53486d548008",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7cb2c877-9f19-45a5-bdaa-881b0c3e21cb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a948354-2821-44d6-84df-2509db58a2fd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dad21093-e715-4b54-af25-9bcaaa93e430",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4fe60bff-afdc-4db8-aab2-e0fcca38324a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "847f6d6a-1042-4b65-8fff-12b1a20c7e30",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81ff76f8-7550-4690-89dd-99a03a945648",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "048f6771-a2b7-46f7-9767-4a448d79b46e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80d48017-1f11-422f-93be-8a93f01a6a42",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b562415-a9ba-4b42-97e5-9671b3ce4cef",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de88657e-3a43-4ac3-8d97-1cb18470a079",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "035d3508-06a2-45b1-bb11-d72bf17338cb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ml",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
